/**
 * 
 */
package com.xth8013.as.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.google.gson.Gson;
import com.xth8013.as.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xth8013.as.domain.User;
import com.xth8013.as.domain.UserConfigParam;
import com.xth8013.as.service.UserService;
import com.xth8013.as.util.ObjectUtils;


/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年4月14日 下午11:14:33 
 * 
 */
@Controller
@RequestMapping("/mainController")
public class MainController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping("/toMainMenu")
	public String toMainMenu(ModelMap modelMap, HttpServletRequest request) throws Exception {
		User user = (User) request.getSession().getAttribute("loginedUser");
		modelMap.put("user", user);
		Gson gson = new Gson();
		UserConfigParam configParam = (UserConfigParam) request.getSession().getAttribute("configParam");
		modelMap.put("configParamJson",gson.toJson(configParam));

		// 若是可折叠菜单，则直接加载数据
		if (ObjectUtils.isNotBlank(configParam) && "accordion".equals(configParam.getLeftMenu())) {
			List<Map<String, Object>> userMenuTreeData = userService.getUserMenuTreeDataById(user.getUserId(), null);
			List<Map<String, Object>> parentList = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> rowMap : userMenuTreeData) {
				String parentId = String.valueOf(rowMap.get("parentId"));
				if (parentId.equals("0")) {
					parentList.add(rowMap);
				}
			}
			modelMap.put("parentListJson", gson.toJson(parentList));
			modelMap.put("parentList", parentList);
		}

		return "main";
	}

	@RequestMapping("/treeMainMenu")
	public String mainTreeMenu(ModelMap modelMap, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("loginedUser");
		modelMap.put("user", user);
		
		return "main_tree_menu";
	}

	@RequestMapping("/accordionMainMenu")
	public String loadUserList(ModelMap modelMap, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("loginedUser");
		modelMap.put("user", user);

		List<Map<String, Object>> userMenuTreeData = userService.getUserMenuTreeDataById(user.getUserId(), null);
		List<Map<String, Object>> parentList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> rowMap : userMenuTreeData) {
			String parentId = String.valueOf(rowMap.get("parentId"));
			if (parentId.equals("0")) {
				parentList.add(rowMap);
			}
		}
		modelMap.put("parentList", parentList);
		System.out.println(parentList);
		return "main_accordion_menu";
	}

	@RequestMapping(value = "/loadUserMenu")
	@ResponseBody
	public List<Map<String, Object>> loadUserMenu(HttpServletRequest request) {
		/**
		 * 1：从Session中取出用户对象
		 */
		User user = (User) request.getSession().getAttribute("loginedUser");

		/**
		 * 获取请求参数是否传递的父类的ID号
		 */
		String parentId = request.getParameter("parentId");

		List<Map<String, Object>> userMenuTreeData = userService.getUserMenuTreeDataById(user.getUserId(), parentId);

		return userMenuTreeData;

	}

	@RequestMapping(value = "/welcomePage")
	public String toWelcomePage() {
		return "welcome_page";
	}

}
